﻿using System;

namespace XExcel.Validations
{
    public class DateRangeValidate : Validate
    {
        public static IValidate Default = new DateRangeValidate();
        private DateTime _datemin = new DateTime(1970, 1, 1);
        private DateTime _datemax = new DateTime(2999, 12, 31);

        public DateRangeValidate(DateTime? datemin = null, DateTime? datemax = null, string message = null)
        {
            _datemin = datemin ?? _datemin;
            _datemax = datemax ?? _datemax;
            Message = message ?? $"日期必须在时间范围({_datemin:yyyy-MM-dd}-{_datemax:yyyy-MM-dd})内";
        }

        public override bool Test(object val)
        {
            if(val is DateTime dt)
            {
                if(dt < _datemin || dt > _datemax)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
